Today we will introduce conditional expressions. We mentioned this concept in the previous chapter. In order to deal with the many complex problems that may be encountered in different programs, conditional judgment statements are very useful. Simply put, when the statement in the if condition is true, the corresponding code is executed, otherwise the judgment is exited. In this chapter, the concepts of logic and comparison operators from Day 7 will be used extensively. If you are not familiar with this part, you can go back to the previous section to review it!
If there are several types of conditional judgments, the simplest one is to use an If statement to summarize the conditions that the program needs to be executed.```
/* if(condition){
If the condition is True,executed the code.
} */
int temperature = 29;
if(temperature < 30){
System.out.println("The weather was cozy today."); //If(true),so print it out.
}
If you want to determine different conditions, you can also use multiple Ifs at once: ```
int grade = 88;
if(grade >= 90 && grade <= 100)
System.out.println("You got A+.");
if(grade >= 85 && grade <= 89)
System.out.println("You got A.");
if(grade >= 80 && grade <= 84)
System.out.println("You got A-.");
We can observe from the above code that although it can achieve the same effect as we want, once there are more conditions to be judged, the readability of the program will deteriorate. Therefore, we can use the Else statement (which is a group with If), and when its condition is judged to be False, the code in else will be executed.
/*多個if判斷改寫*/
int grade = 84;
if(grade < 90)
{
if(grade < 85)
System.out.println("You got A-.");
else
System.out.println("You got A.");
}
else System.out.println("You got A+.");
When the conditions for judgment are very similar, a “nested structure” can be used to reduce unnecessary judgments.
int grade = 72;
if (grade > 90 && grade <= 100)
System.out.println("Excellent!");
else if (grade > 80 && grade <= 90)
System.out.println("Good!");
else
System.out.println("Not bad.");
If...else is, in short, a short version of conditional judgment. It can replace a large section of code and is mostly used to replace simple if...else statements.
/* variable = (condition) ? (ifexpressionTrue) : (ifexpressionFalse);*/
int grade = 100;
char GPA;
GPA = (grade >= 90) ? ('A') : ('B');
System.out.println(GPA); /*Outputs : A*/
Next, we will introduce another conditional judgment, Switch. The biggest difference between Switch and if…else is that Switch covers more cases. A common application is to select the content to be executed through our own input.
The following example provides a simple song list of function options, and the function to be used is selected through our input. (The function writing part has not been introduced here, and more content will follow.)
char grade = 'B';
switch(grade) {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
System.out.println("Great!");
break;
case 'C' :
System.out.println("Good!");
break;
case 'D' :
System.out.println("Not bad.");
break;
case 'E' :
System.out.println("Don't give up.");
break;
default :
System.out.println("Wrong data!!!");
break;
}
:
break
: indicates the end of a case. If you forget to add it, the compiler will keep executing until it encounters break.default
: the case that will be executed when none of the above conditions are met.import java.util.Scanner;
public class web {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("請輸入功能編號: ");
int function;
function = input.nextInt();
while(true){
switch(function)
{
case 1 :
System.out.println("歌手查詢");
break;
case 2 :
System.out.println("歌名查詢");
break;
case 3 :
System.out.println("更改資訊");
break;
case 4 :
System.out.println("隨機撥放");
break;
default :
System.out.println("輸入錯誤");
break;
}
}
}
}